package de.metalcon.gwt.client.modules;

import java.util.ArrayList;
import java.util.Map;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;

import de.metalcon.gwt.client.mainDesign.Box;
import de.metalcon.gwt.client.pages.StreamPage;
import de.metalcon.gwt.client.pages.UserPage;
import de.metalcon.gwt.shared.ContentTransmitter;

public class StreamModule extends ADynamicModule {
	public StreamModule(String type, String key) {
		addReuqestVar("type", type);
		addReuqestVar("key", key);
	}

	@Override
	protected void generateContent() {
		ContentTransmitter transmitter = getContentTransmitter();
		
		Box box = new Box();

		Label caption = new Label("Request vars");
		box.add(caption);
		
		for (Map.Entry<String, Object> entry : transmitter.getAllPairs()) {
			Label testContent = new Label(entry.getKey() + " : " + entry.getValue());
			box.add(testContent);
		}
		ArrayList<ContentTransmitter> tmp = null;
		if (transmitter.getString("type").equals("user")){
			caption.setText("anzahl an traversals = " + transmitter.getString("tcnt"));
			box.add(caption);
			tmp = transmitter.getList("band");
			if(tmp!= null){
			for (ContentTransmitter ct : tmp){
				Hyperlink link = new Hyperlink(ct.getString("key"), StreamPage.generateHistoryToken(new String[]{"band",ct.getString("key")}));
				if (ct.getString("fans")!=null){
					link.setText(ct.getString("key") + " mit (" + ct.getString("fans") + ") fans" );
				}
				
				box.add(link);
				
/*				Label m = new Label("similar bands to " + ct.getString("key")+ " are:");
				box.add(m);
				ArrayList<ContentTransmitter> simBands = ct.getList("simBands");
				for (ContentTransmitter simBand : simBands){
					link = new Hyperlink(simBand.getString("key"), StreamPage.generateHistoryToken(new String[]{"band",simBand.getString("key")}));
					box.add(link);
				}*/
				HTML html = new HTML(".<br>.<br>");
				box.add(html);
				
			}
			} else {
				Label l = new Label("No entry found!");
				box.add(l);
			}

		}
		else if(transmitter.getString("type").equals("band")){
			tmp = transmitter.getList("user");
			for (ContentTransmitter ct : tmp){
				Hyperlink link = new Hyperlink(ct.getString("key"), StreamPage.generateHistoryToken(new String[]{"user",ct.getString("key")}));
				box.add(link);
				if (ct.getString("bands")!=null){
					Label count = new Label(" (" + ct.getString("bands") + ")" );
					box.add(count);
				}

			}

		}
		add(box);
	}
}
